package Management;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellRenderer;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.*;
import java.sql.Date;

/* Used by InternalFrameDemo.java. */
public class ProfileManager extends JInternalFrame {
    
	static int openFrameCount = 0;
    static final int xOffset = 30, yOffset = 30;
    private boolean DEBUG = false;

    public ProfileManager() 
    {
				        super("Profile Manager", 
				              true, //resizable
				              true, //closable
				              true, //maximizable
				              true);//iconifiable
				
				        //...Create the GUI and put it in the window...
				
				        //...Then set the window size or call pack...
				        setSize(1300,600);
				
				        //Set the window's location.
				        setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
				        setLayout(new BorderLayout());
				        
				        String[] columnNames = {"First Name", "Last Name","Date of Birth", "age", "Street Address", "Grade"};
				
				Object[][] data = {
						 {"Ammmm", "Smith",new Date(86,10,16), new Integer(5), new Boolean(false), new Integer(1)},
						        {"John", "Doe",new Date(93,1,3), new Integer(8), new Boolean(true), new Integer(3)},
						       {"Sue", "Black",new Date(84,4,3), new Integer(7), new Boolean(false), new Integer(7-5)},
						        {"Jane", "White",new Date(94,6,27), new Integer(9), new Boolean(true), new Integer(9-5)},
						       {"Joe", "Brown",new Date(97,7,19), new Integer(10), new Boolean(false), new Integer(10-5)}				
				};

				final JTable table = new JTable(data, columnNames);
				table.setPreferredScrollableViewportSize(new Dimension(500, 70));
				table.setFillsViewportHeight(true);
				table.setColumnSelectionAllowed(true);
				table.setRowSelectionAllowed(true);
				table.setAutoCreateRowSorter(true);
               
				/*searchField.addActionListener(new ActionListener() 
				{
					public void actionPerformed(ActionEvent arg0) 
					{
						String value= searchField.getText();

				         for(int row= 0;row <= table.getRowCount() -1;row++)
				         {
				    	      for(int col = 0;col <= table.getColumnCount()-1; col++)
				    	      {
				    		        if (value.equals(table.getValueAt(row, col))) 
				    		        {
			                           table.scrollRectToVisible(table.getCellRect(row, 0, true));
			                           table.setRowSelectionInterval(row, row);
			                           for (int i = 0; i <= table.getColumnCount() - 1; i++) 
			                           {
			                            // table.getColumnModel().getColumn(i).setCellRenderer(new HightlightRenderer());
			                            }
			                        }
				    	       }
				          }				     						
			        }				
				});*/
							
				if (DEBUG) 
				{
				      table.addMouseListener(new MouseAdapter() {
				           public void mouseClicked(MouseEvent e) {
				            printDebugData(table);
				            }
				       });
				}
				//create new pane and place all the search bars there
                JPanel pane= new JPanel();
                JLabel label;
                
                pane.setLayout(new GridBagLayout());
                GridBagConstraints c = new GridBagConstraints();
                boolean shouldFill=false;
				if (shouldFill) {
                                //natural height, maximum width
                                c.fill = GridBagConstraints.HORIZONTAL;
                }

                label = new JLabel("First Name");
				final JTextField searchField1= new JTextField();
				final JTextField searchField2= new JTextField();
				final JTextField searchField3= new JTextField();
				final JTextField searchField4= new JTextField();
				final JTextField searchField5= new JTextField();

                boolean shouldWeightX=false;
				if (shouldWeightX) {
                                   c.weightx = 0.5;
                }
                c.fill = GridBagConstraints.HORIZONTAL;
                c.weightx = 0.5;
                c.gridx = 0;
                c.gridy = 0;
                pane.add(label, c);

                label = new JLabel("Last Name");
                c.fill = GridBagConstraints.HORIZONTAL;
                c.weightx = 0.5;
                c.gridx = 1;
                c.gridy = 0;
                pane.add(label, c);

                label = new JLabel("Age");
                c.fill = GridBagConstraints.HORIZONTAL;
                c.weightx = 0.5;
                c.gridx = 2;
                c.gridy = 0;
                pane.add(label, c);            

                label = new JLabel("Date of Birth");
                c.fill = GridBagConstraints.HORIZONTAL;
                c.weightx = 0.5;
                c.gridx = 3;
                c.gridy = 0;
                pane.add(label, c);
                

                label = new JLabel("Street Address");
                c.fill = GridBagConstraints.HORIZONTAL;
                c.weightx = 0.5;
                c.gridx = 4;
                c.gridy = 0;
                pane.add(label, c);

                c.fill = GridBagConstraints.HORIZONTAL;
                c.ipady = 8;      //make this component tall
                c.weightx = 0.0;
                c.gridwidth = 1;
                c.gridx = 0;
                c.gridy = 1;
				searchField1.setBounds(10,100,150,20);
                pane.add(searchField1, c);
                
                c.fill = GridBagConstraints.HORIZONTAL;
                c.ipady = 8;      //make this component tall
                c.weightx = 0.0;
                c.gridwidth = 1;
                c.gridx = 1;
                c.gridy = 1;
				searchField2.setBounds(10,100,150,20);
                pane.add(searchField2, c);
                
                c.fill = GridBagConstraints.HORIZONTAL;
                c.ipady = 8;      //make this component tall
                c.weightx = 0.0;
                c.gridwidth = 1;
                c.gridx = 2;
                c.gridy = 1;
				searchField3.setBounds(10,100,150,20);
                pane.add(searchField3, c);
                
                c.fill = GridBagConstraints.HORIZONTAL;
                c.ipady = 8;      //make this component tall
                c.weightx = 0.0;
                c.gridwidth = 1;
                c.gridx = 3;
                c.gridy = 1;
				searchField4.setBounds(10,100,150,20);
                pane.add(searchField4, c);
                
                c.fill = GridBagConstraints.HORIZONTAL;
                c.ipady = 8;      //make this component tall
                c.weightx = 0.0;
                c.gridwidth = 1;
                c.gridx = 4;
                c.gridy = 1;
				searchField5.setBounds(10,100,150,20);
                pane.add(searchField5, c);
                          
              //  pane.add(searchField);
				add(pane, BorderLayout.PAGE_START);
				//Create the scroll pane and add the table to it.			
				JScrollPane scrollPane = new JScrollPane(table);
				//Add the scroll pane to this panel.
				add(scrollPane,BorderLayout.CENTER);
       }

private void printDebugData(JTable table) {
int numRows = table.getRowCount();
int numCols = table.getColumnCount();
javax.swing.table.TableModel model = table.getModel();

System.out.println("Value of data: ");
    for (int i=0; i < numRows; i++) {
        System.out.print("row " + i + ":");
             for (int j=0; j < numCols; j++) 
             {
                 System.out.print("  " + model.getValueAt(i, j));
             }
             System.out.println();
     }
    
System.out.println("--------------------------");
}
    
}
